package com.wcgksoft.controller.att_management;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.Att_management.Att_management;
import com.wcgksoft.entity.Att_management.CheckWork_Month;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.Att_management.Att_managementService;
import com.wcgksoft.service.Att_management.CheckWork_MonthService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 周权
 * @Date: 2020/03/12/12:04
 * @Description:考勤统计
 */
@Controller
@RequestMapping(value = "system/att_checkwork")
public class Att_checkworkController {
    @Autowired
    private Att_managementService att_managementService;
    @Autowired
    private CheckWork_MonthService checkWork_monthService;

    /*
     * @Author: 周权
     * @Description:考勤系统列表页面layui的table加载数据(施工账号)
     * @Param:
     * @create: 2020/3/11 10:29
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "a_tid", required = false) Integer a_tid,
                         @RequestParam(value = "e_id", required = false) Integer e_id,
                         @RequestParam(value = "role_id", required = false) Integer role_id,
                         @RequestParam(value = "area_id", required = false) Integer area_id,
                         @RequestParam(value = "page", required = false) Integer page,
                         @RequestParam(value = "limit", required = false) Integer limit,
                         @RequestParam(value = "starttime", required = false) String starttime,
//                         @RequestParam(value = "endtime", required = false) String endtime,
                         @RequestParam(value = "a_name", required = false) String a_name) throws ParseException {
        DetachedCriteria criteria = DetachedCriteria.forClass(Att_management.class);

        if (null != a_name && !"".equals(a_name)) {
            criteria.add(Restrictions.eq("a_name", a_name));
        }
        if (null != starttime && !"".equals(starttime)) {
            criteria.add(Restrictions.sqlRestriction("DATE_FORMAT(createDate,'%Y-%m')= '" + starttime + "'"));
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
//            Date parse = sdf.parse(starttime);
//            criteria.add(Restrictions.between("a_createDate", parse,parse));
        }
//        if ((null!=starttime&&!"".equals(starttime)) && (null!=endtime&&!"".equals(endtime))) {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
//            Date parse = sdf.parse(starttime);
//            Date parse2 = sdf.parse(endtime);
//            Calendar c1 = Calendar.getInstance();
//            c1.setTime(parse2);
//            int mon = c1.get(Calendar.MONTH)+2;
//            int year = c1.get(Calendar.YEAR);
//            String str = String.valueOf(mon);
//            String str2 = String.valueOf(year);
//            String newDate = str2+"-"+str;
//            Date newparse = sdf.parse(newDate);
//            criteria.add(Restrictions.between("a_createDate", parse, newparse));
//        }
//        if ((null!=starttime&&!"".equals(starttime)) &&( null==endtime||"".equals(endtime))){
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
//            Date parse = sdf.parse(starttime);
//            Date parse2 = sdf.parse("2099-12");
//            criteria.add(Restrictions.between("a_createDate", parse, parse2));
//        }
//        if ((null!=endtime&&!"".equals(endtime)) && (null==starttime||"".equals(starttime))){
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
//            Date parse = sdf.parse(endtime);
//            Date parse2 = sdf.parse("1997-12");
//            Calendar c1 = Calendar.getInstance();
//            c1.setTime(parse);
//            int mon = c1.get(Calendar.MONTH)+2;
//            int year = c1.get(Calendar.YEAR);
//            String str = String.valueOf(mon);
//            String str2 = String.valueOf(year);
//            String newDate = str2+"-"+str;
//            Date newparse = sdf.parse(newDate);
//            criteria.add(Restrictions.between("a_createDate", parse2, newparse));
//        }
        if (role_id == 2) {
            criteria.add(Restrictions.eq("a_eid", e_id));
        } else if (role_id == 3) {
            criteria.add(Restrictions.eq("a_eid", e_id));
            criteria.add(Restrictions.eq("a_area_id", area_id));
        } else if (role_id == 4) {
            criteria.add(Restrictions.eq("a_tid", a_tid));
        }
        criteria.addOrder(Order.asc("a_createDate"));
        criteria.add(Restrictions.eq("a_status", 0));
        List<Att_management> byConditions = att_managementService.findByConditions(criteria, page, limit);
        return Layui.data(0, "查询成功", att_managementService.getTotalCount(), byConditions);
    }

    /*
     * @Author: 周权
     * @Description:考勤系统列表表头(施工账号)
     * @Param:
     * @create: 2020/3/11 10:29
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "findCols")
    public Layui findCols(@RequestParam(value = "a_tid", required = false) Integer a_tid,
                          @RequestParam(value = "starttime", required = false) String starttime,
                          @RequestParam(value = "a_name", required = false) String a_name,
                          @RequestParam(value = "e_id", required = false) Integer e_id,
                          @RequestParam(value = "area_id", required = false) Integer area_id,
                          @RequestParam(value = "role_id", required = false) Integer role_id) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Att_management.class);
        criteria.addOrder(Order.asc("a_createDate"));
        criteria.add(Restrictions.eq("a_status", 0));
        if (role_id == 2) {
            if (null != a_name && !"".equals(a_name)) {
                criteria.add(Restrictions.like("a_name", a_name, MatchMode.ANYWHERE));
            }
            if (null != starttime && !"".equals(starttime)) {
                criteria.add(Restrictions.sqlRestriction("DATE_FORMAT(createDate,'%Y-%m')= '" + starttime + "'"));
            }
            criteria.add(Restrictions.eq("a_eid", e_id));
            List<Att_management> byConditions = att_managementService.findByConditions(criteria);
            return Layui.data(0, "查询成功", att_managementService.getTotalCount(), byConditions);
        } else if (role_id == 3) {
            if (null != a_name && !"".equals(a_name)) {
                criteria.add(Restrictions.like("a_name", a_name, MatchMode.ANYWHERE));
            }
            if (null != starttime && !"".equals(starttime)) {
                criteria.add(Restrictions.sqlRestriction("DATE_FORMAT(createDate,'%Y-%m')= '" + starttime + "'"));
            }
            criteria.add(Restrictions.eq("a_eid", e_id));
            criteria.add(Restrictions.eq("a_area_id", area_id));
            List<Att_management> byConditions = att_managementService.findByConditions(criteria);
            return Layui.data(0, "查询成功", att_managementService.getTotalCount(), byConditions);
        } else if (role_id == 4) {
            criteria.add(Restrictions.eq("a_tid", a_tid));
            List<Att_management> byConditions = att_managementService.findByConditions(criteria);
            return Layui.data(0, "查询成功", att_managementService.getTotalCount(), byConditions);
        }
        return Layui.data(0, null, null);
    }

    /*
     * @Author: 周权
     * @Description:某施工队伍某月逐日的人数柱状图
     * @Param:
     * @create: 2020/3/12 15:44
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "statis")
    public HashMap statis(@RequestParam(value = "a_tid", required = false) Integer a_tid,
                          @RequestParam(value = "e_id", required = false) Integer e_id,
                          @RequestParam(value = "area_id", required = false) Integer area_id,
                          @RequestParam(value = "role_id", required = false) Integer role_id,
                          @RequestParam(value = "starttime", required = false) String starttime,
                          @RequestParam(value = "a_name", required = false) String a_name) throws ParseException {
        DetachedCriteria criteria = DetachedCriteria.forClass(Att_management.class);
        if (null != a_name && !"".equals(a_name)) {
            criteria.add(Restrictions.like("a_name", a_name, MatchMode.ANYWHERE));
        }
        if (null != starttime && !"".equals(starttime)) {
            criteria.add(Restrictions.sqlRestriction("DATE_FORMAT(createDate,'%Y-%m')= '" + starttime + "'"));
        }
        if (role_id == 2) {
            criteria.add(Restrictions.eq("a_eid", e_id));
        } else if (role_id == 3) {
            criteria.add(Restrictions.eq("a_eid", e_id));
            criteria.add(Restrictions.eq("a_area_id", area_id));
        } else if (role_id == 4) {
            criteria.add(Restrictions.eq("a_tid", a_tid));
        }
        criteria.add(Restrictions.eq("a_status", 0));
        List<Att_management> byConditions = att_managementService.findByConditions(criteria);
        if (byConditions.size() > 0) {
            HashMap map = new HashMap<>();
            List<Integer> list1 = new ArrayList<>();
            List<String> list2 = new ArrayList<>();
            if (null != a_name && !"".equals(a_name)) {
                String teamName = null;
                for (int l = 0; l < byConditions.size(); l++) {
                    if (null == teamName) {
                        teamName = byConditions.get(l).getA_name();
                    }
                }
                map.put("team", teamName);//map装入查询的队伍名称
            }

            if (null != starttime && !"".equals(starttime)) {
                String lastDayOfMonth = getLastDayOfMonth(starttime);
                Calendar cal = Calendar.getInstance();//新建日历类
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//格式化年-月-日
                Date parse = dateFormat.parse(lastDayOfMonth);//查询月份显示查询日期柱状图
                cal.setTime(parse);//日历设置当前时间
                int year = cal.get(Calendar.YEAR);//获得年
                int month = cal.get(Calendar.MONTH) + 1;//获得月
                String yea = String.valueOf(year);//年转字符串
                String mon = String.valueOf(month);//月转字符串
                int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);//获得当月最大日期
                int minDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);//获得当月最小日期
                for (int i = minDay; i <= maxDay; i++) {//循环当月所有天数
                    String workday;
                    if (i < 10) {//如果日期当天小于10（一位数）
                        if (month < 10) {//如果当前月份小于十（一位数）
                            workday = yea + "-0" + mon + "-0" + i;//拼接字符串 xxxx-0x-0x
                        } else {
                            workday = yea + "-" + mon + "-0" + i;//月份两位数 xxxx-xx-0x
                        }
                    } else {//日期当天大于等于10（两位数）
                        if (month < 10) {//如果当前月份小于十（一位数）
                            workday = yea + "-0" + mon + "-" + i;//拼接字符串 xxxx-0x-xx
                        } else {
                            workday = yea + "-" + mon + "-" + i;//月份两位数 xxxx-xx-xx
                        }
                    }
                    DetachedCriteria c1 = DetachedCriteria.forClass(Att_management.class);//离线查询考勤
                    c1.add(Restrictions.sqlRestriction("DATE_FORMAT(createDate,'%Y-%m-%d')= '" + workday + "'"));//将拼接的字符串与数据库考勤日期字段对比
                    if (null != a_name && !"".equals(a_name)) {
                        c1.add(Restrictions.like("a_name", a_name, MatchMode.ANYWHERE));
                    }
                    if (role_id == 2) {
                        c1.add(Restrictions.eq("a_eid", e_id));
                    } else if (role_id == 3) {
                        c1.add(Restrictions.eq("a_eid", e_id));
                        c1.add(Restrictions.eq("a_area_id", area_id));
                    } else if (role_id == 4) {
                        c1.add(Restrictions.eq("a_tid", a_tid));
                    }
                    c1.add(Restrictions.eq("a_status", 0));
                    List<Att_management> by1 = att_managementService.findByConditions(c1);//获得符合条件集合
                    if (by1.size() > 0) {//有符合条件数据
                        int sum = 0;//定义sum用于存放当天出勤人数总值
                        for (int s = 0; s < by1.size(); s++) {//循环考勤集合
                            if (null != by1.get(s).getA_pm_num()) {//下午签到人数不为空
                                sum += by1.get(s).getA_pm_num();//出勤人数累加
                            } else {
                                sum = sum + 0;//下午未签到将不计算出勤人数
                            }
                        }
                        list1.add(sum);//将总出勤数装入list1
                    } else {//无符合条件数据
                        list1.add(0);//当月无签到数据 每天都存0
                    }
                    String md = mon + "-" + i;//拼接字符串 xx-x||xx-xx
                    list2.add(md);//循环装入list2（日期集合）
                }
                map.put("worklist", list1);//map装入list1
                map.put("daylist", list2);//map装入list2
                map.put("month", mon);//map装入查询的月份
                return map;
            } else {
                Calendar cal = Calendar.getInstance();//新建日历类
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//格式化年-月-日
                Date parse = dateFormat.parse(dateFormat.format(new Date()));//不查询月份默认显示新建日期（当月）柱状图
                cal.setTime(parse);//日历设置当前时间
                int year = cal.get(Calendar.YEAR);//获得年
                int month = cal.get(Calendar.MONTH) + 1;//获得月
                String yea = String.valueOf(year);//年转字符串
                String mon = String.valueOf(month);//月转字符串
                int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);//获得当月最大日期
                int minDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);//获得当月最小日期
                for (int i = minDay; i <= maxDay; i++) {//循环当月所有天数
                    String workday = null;
                    if (i < 10) {//如果日期当天小于10（一位数）
                        if (month < 10) {//如果当前月份小于十（一位数）
                            workday = yea + "-0" + mon + "-0" + i;//拼接字符串 xxxx-0x-0x
                        } else {
                            workday = yea + "-" + mon + "-0" + i;//月份两位数 xxxx-xx-0x
                        }
                    } else {//日期当天大于等于10（两位数）
                        if (month < 10) {//如果当前月份小于十（一位数）
                            workday = yea + "-0" + mon + "-" + i;//拼接字符串 xxxx-0x-xx
                        } else {
                            workday = yea + "-" + mon + "-" + i;//月份两位数 xxxx-xx-xx
                        }
                    }
                    DetachedCriteria c1 = DetachedCriteria.forClass(Att_management.class);//离线查询考勤
                    c1.add(Restrictions.sqlRestriction("DATE_FORMAT(createDate,'%Y-%m-%d')= '" + workday + "'"));//将拼接的字符串与数据库考勤日期字段对比
                    if (null != a_name && !"".equals(a_name)) {
                        c1.add(Restrictions.like("a_name", a_name, MatchMode.ANYWHERE));
                    }
                    if (role_id == 2) {
                        c1.add(Restrictions.eq("a_eid", e_id));
                    } else if (role_id == 3) {
                        c1.add(Restrictions.eq("a_eid", e_id));
                        c1.add(Restrictions.eq("a_area_id", area_id));
                    } else if (role_id == 4) {
                        c1.add(Restrictions.eq("a_tid", a_tid));
                    }
                    c1.add(Restrictions.eq("a_status", 0));
                    List<Att_management> by1 = att_managementService.findByConditions(c1);//获得符合条件集合
                    if (by1.size() > 0) {//有符合条件数据
                        int sum = 0;//定义sum用于存放当天出勤人数总值
                        for (int s = 0; s < by1.size(); s++) {//循环考勤集合
                            if (null != by1.get(s).getA_pm_num()) {//下午签到人数不为空
                                sum += by1.get(s).getA_pm_num();//出勤人数累加
                            } else {
                                sum = sum + 0;//下午未签到将不计算出勤人数
                            }
                        }
                        list1.add(sum);//将总出勤数装入list1
                    } else {//无符合条件数据
                        list1.add(0);//当月无签到数据 每天都存0
                    }
                    String md = mon + "-" + i;//拼接字符串 xx-x||xx-xx
                    list2.add(md);//循环装入list2（日期集合）
                }
                map.put("worklist", list1);//map装入list1
                map.put("daylist", list2);//map装入list2
                map.put("month", mon);//map装入查询的月份
                return map;
            }
        } else {
            HashMap map = new HashMap<>();
            List<Integer> list1 = new ArrayList<>();
            List<String> list2 = new ArrayList<>();
            Calendar cal = Calendar.getInstance();//新建日历类
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//格式化年-月-日
            Date parse = dateFormat.parse(dateFormat.format(new Date()));//不查询月份默认显示新建日期（当月）柱状图
            cal.setTime(parse);//日历设置当前时间
            int month = cal.get(Calendar.MONTH) + 1;//获得月
            String mon = String.valueOf(month);//月转字符串
            int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);//获得当月最大日期
            int minDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);//获得当月最小日期
            for (int i = minDay; i <= maxDay; i++) {//循环当月所有天数
                list1.add(0);//当月无签到数据 每天都存0
                String md = mon + "-" + i;//拼接字符串 xx-x||xx-xx
                list2.add(md);//循环装入list2（日期集合）
            }
            map.put("worklist", list1);//map装入list1
            map.put("daylist", list2);//map装入list2
            map.put("month", mon);//map装入查询的月份
            map.put("team", "暂无施工队");//map装入查询的队伍名称
            return map;
        }
    }

    /*
     * @Author: 周权
     * @Description:获得当前月份天数
     * @Param:
     * @create: 2020/3/12 16:57
     * @return:
     */
    public static String getLastDayOfMonth(String yearMonth) {
        int year = Integer.parseInt(yearMonth.split("-")[0]);  //年
        int month = Integer.parseInt(yearMonth.split("-")[1]); //月
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        // cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.MONTH, month); //设置当前月的上一个月
        // 获取某月最大天数
        //int lastDay = cal.getActualMaximum(Calendar.DATE);
        int lastDay = cal.getMinimum(Calendar.DATE); //获取月份中的最小值，即第一天
        // 设置日历中月份的最大天数
        //cal.set(Calendar.DAY_OF_MONTH, lastDay);
        cal.set(Calendar.DAY_OF_MONTH, lastDay - 1); //上月的第一天减去1就是当月的最后一天
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    /*
     * @Author: 周权
     * @Description: 月平均人数列表表头
     * @Param:
     * @create: 2020/3/16 14:05
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "findCols_month")
    public HashMap findCols_month(@RequestParam(value = "e_id", required = false) Integer e_id,
                                  @RequestParam(value = "starttime", required = false) String starttime,
                                  @RequestParam(value = "a_name", required = false) String a_name,
                                  @RequestParam(value = "role_id", required = false) Integer role_id,
                                  @RequestParam(value = "area_id", required = false) Integer area_id) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CheckWork_Month.class);
        if (role_id == 2) {
            if (null != a_name && !"".equals(a_name)) {
                criteria.add(Restrictions.like("c_name", a_name, MatchMode.ANYWHERE));
            }
            if (null != starttime && !"".equals(starttime)) {
                criteria.add(Restrictions.like("c_mon", starttime, MatchMode.ANYWHERE));
            }
            criteria.add(Restrictions.eq("c_eid", e_id));
            List<CheckWork_Month> byConditions = checkWork_monthService.findByConditions(criteria);
            return Layui.data(0, "查询成功", checkWork_monthService.getTotalCount(), byConditions);
        } else if (role_id == 3) {
            if (null != a_name && !"".equals(a_name)) {
                criteria.add(Restrictions.like("c_name", a_name, MatchMode.ANYWHERE));
            }
            if (null != starttime && !"".equals(starttime)) {
                criteria.add(Restrictions.like("c_mon", starttime, MatchMode.ANYWHERE));
            }
            criteria.add(Restrictions.eq("c_eid", e_id));
            criteria.add(Restrictions.eq("c_area_id", area_id));
            List<CheckWork_Month> byConditions = checkWork_monthService.findByConditions(criteria);
            return Layui.data(0, "查询成功", checkWork_monthService.getTotalCount(), byConditions);
        }
        return Layui.data(0, null, null);
    }

    /*
     * @Author: 周权
     * @Description:考勤系统月平均值页面layui的table加载数据
     * @Param:
     * @create: 2020/3/17 10:29
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "findCheck_month")
    public HashMap<String, Object> findCheck_month(
            @RequestParam(value = "e_id", required = false) Integer e_id,
            @RequestParam(value = "role_id", required = false) Integer role_id,
            @RequestParam(value = "area_id", required = false) Integer area_id,
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "starttime", required = false) String starttime,
            @RequestParam(value = "a_name", required = false) String a_name) throws ParseException {
        DetachedCriteria criteria = DetachedCriteria.forClass(CheckWork_Month.class);
        if (null != a_name && !"".equals(a_name)) {
            criteria.add(Restrictions.like("c_name", a_name, MatchMode.ANYWHERE));
        }
        if (null != starttime && !"".equals(starttime)) {
            criteria.add(Restrictions.like("c_mon", starttime, MatchMode.ANYWHERE));
        }
        if (role_id == 2) {
            criteria.add(Restrictions.eq("c_eid", e_id));
        } else if (role_id == 3) {
            criteria.add(Restrictions.eq("c_eid", e_id));
            criteria.add(Restrictions.eq("c_area_id", area_id));
        }
        criteria.addOrder(Order.asc("c_name"));
        criteria.addOrder(Order.asc("c_mon"));
        List<CheckWork_Month> byConditions = checkWork_monthService.findByConditions(criteria, page, limit);
        return Layui.data(0, "查询成功", checkWork_monthService.getTotalCount(), byConditions);
    }

    /*
     * @Author: 周权
     * @Description:月平均人数柱状图
     * @Param:
     * @create: 2020/3/16 22:38
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "statisMon")
    public HashMap statisMon(
            @RequestParam(value = "e_id", required = false) Integer e_id,
            @RequestParam(value = "area_id", required = false) Integer area_id,
            @RequestParam(value = "role_id", required = false) Integer role_id,
            @RequestParam(value = "starttime", required = false) String starttime,
            @RequestParam(value = "a_name", required = false) String a_name) throws ParseException {
        DetachedCriteria criteria = DetachedCriteria.forClass(CheckWork_Month.class);
        if (null != a_name && !"".equals(a_name)) {
            criteria.add(Restrictions.like("c_name", a_name, MatchMode.ANYWHERE));
        }
        if (null != starttime && !"".equals(starttime)) {
            criteria.add(Restrictions.like("c_mon", starttime, MatchMode.ANYWHERE));
        }
        if (role_id == 2) {
            criteria.add(Restrictions.eq("c_eid", e_id));
        } else if (role_id == 3) {
            criteria.add(Restrictions.eq("c_eid", e_id));
            criteria.add(Restrictions.eq("c_area_id", area_id));
        }
        List<CheckWork_Month> byConditions = checkWork_monthService.findByConditions(criteria);
        ArrayList<Double> ave = new ArrayList<>();
        HashMap map = new HashMap<>();
        if (byConditions.size() > 0) {
            if (null != a_name && !"".equals(a_name)) {
                String teamName = null;
                for (int l = 0; l < byConditions.size(); l++) {
                    if (null == teamName) {
                        teamName = byConditions.get(l).getC_name();
                    }
                }
                map.put("name", teamName);//map装入查询的队伍名称
            }
            for (CheckWork_Month checkWork_month : byConditions) {
                Double c_ave = checkWork_month.getC_ave();
                ave.add(c_ave);
            }
            map.put("worklist", ave);
            return map;
        } else {
            for (int i = 1; i <= 12; i++) {
                ave.add(0.0);
            }
            map.put("worklist", ave);
            return map;
        }
    }

    /*
     * @Author: 周权
     * @Description:首页考勤统计曲线图
     * @Param:
     * @create: 2020/3/30 14:23
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "admin_att_static")
    public HashMap statis() throws ParseException {
        DetachedCriteria criteria = DetachedCriteria.forClass(Att_management.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        Session session = SecurityUtils.getSubject().getSession();
        Integer id1 = sysUser.getSysRole().getSysRoleCode().getId();
        if(id1==2){
            Integer id = sysUser.getEnterprise().getId();
            criteria.add(Restrictions.eq("a_eid", id));
        }else if(id1==3){
            Integer id = sysUser.getEnterprise().getId();
            EnterpriseProject enterpriseProject = (EnterpriseProject)session.getAttribute("area");
            Integer id2 = enterpriseProject.getId();
            criteria.add(Restrictions.eq("a_eid", id));
            criteria.add(Restrictions.eq("a_area_id", id2));
        }else if(id1==4){
            Integer id = sysUser.getEnterprise().getId();
            Integer team_id = sysUser.getTeam_id();
            criteria.add(Restrictions.eq("a_eid", id));
            criteria.add(Restrictions.eq("a_tid", team_id));
        }
        criteria.add(Restrictions.eq("a_status", 0));
        List<Att_management> byConditions = att_managementService.findByConditions(criteria);
        if (byConditions.size() > 0) {
            HashMap map = new HashMap<>();
            List<Integer> list1 = new ArrayList<>();
            List<String> list2 = new ArrayList<>();
            Calendar cal = Calendar.getInstance();//新建日历类
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//格式化年-月-日
            Date parse = dateFormat.parse(dateFormat.format(new Date()));//不查询月份默认显示新建日期（当月）柱状图
            cal.setTime(parse);//日历设置当前时间
            int year = cal.get(Calendar.YEAR);//获得年
            int month = cal.get(Calendar.MONTH) + 1;//获得月
            String yea = String.valueOf(year);//年转字符串
            String mon = String.valueOf(month);//月转字符串
            int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);//获得当月最大日期
            int minDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);//获得当月最小日期
            for (int i = minDay; i <= maxDay; i++) {//循环当月所有天数
                String workday = null;
                if (i < 10) {//如果日期当天小于10（一位数）
                    if (month < 10) {//如果当前月份小于十（一位数）
                        workday = yea + "-0" + mon + "-0" + i;//拼接字符串 xxxx-0x-0x
                    } else {
                        workday = yea + "-" + mon + "-0" + i;//月份两位数 xxxx-xx-0x
                    }
                } else {//日期当天大于等于10（两位数）
                    if (month < 10) {//如果当前月份小于十（一位数）
                        workday = yea + "-0" + mon + "-" + i;//拼接字符串 xxxx-0x-xx
                    } else {
                        workday = yea + "-" + mon + "-" + i;//月份两位数 xxxx-xx-xx
                    }
                }
                DetachedCriteria c1 = DetachedCriteria.forClass(Att_management.class);//离线查询考勤
                c1.add(Restrictions.sqlRestriction("DATE_FORMAT(createDate,'%Y-%m-%d')= '" + workday + "'"));//将拼接的字符串与数据库考勤日期字段对比
                if(id1==2){
                    Integer id = sysUser.getEnterprise().getId();
                    criteria.add(Restrictions.eq("a_eid", id));
                }else if(id1==3){
                    Integer id = sysUser.getEnterprise().getId();
                    EnterpriseProject enterpriseProject = (EnterpriseProject)session.getAttribute("area");
                    Integer id2 = enterpriseProject.getId();
                    criteria.add(Restrictions.eq("a_eid", id));
                    criteria.add(Restrictions.eq("a_area_id", id2));
                }else if(id1==4){
                    Integer id = sysUser.getEnterprise().getId();
                    Integer team_id = sysUser.getTeam_id();
                    criteria.add(Restrictions.eq("a_eid", id));
                    criteria.add(Restrictions.eq("a_tid", team_id));
                }
                c1.add(Restrictions.eq("a_status", 0));
                List<Att_management> by1 = att_managementService.findByConditions(c1);//获得符合条件集合
                if (by1.size() > 0) {//有符合条件数据
                    int sum = 0;//定义sum用于存放当天出勤人数总值
                    for (int s = 0; s < by1.size(); s++) {//循环考勤集合
                        if (null != by1.get(s).getA_abs_num()) {//下午签到人数不为空
                            sum += by1.get(s).getA_abs_num();//出勤人数累加
                        } else {
                            sum = sum + 0;//下午未签到将不计算出勤人数
                        }
                    }
                    list1.add(sum);//将总出勤数装入list1
                } else {//无符合条件数据
                    list1.add(0);//当月无签到数据 每天都存0
                }
                String md = mon + "-" + i;//拼接字符串 xx-x||xx-xx
                list2.add(md);//循环装入list2（日期集合）
            }
            map.put("worklist", list1);//map装入list1
            map.put("daylist", list2);//map装入list2
            return map;
        } else {
            HashMap map = new HashMap<>();
            List<Integer> list1 = new ArrayList<>();
            List<String> list2 = new ArrayList<>();
            Calendar cal = Calendar.getInstance();//新建日历类
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//格式化年-月-日
            Date parse = dateFormat.parse(dateFormat.format(new Date()));//不查询月份默认显示新建日期（当月）柱状图
            cal.setTime(parse);//日历设置当前时间
            int month = cal.get(Calendar.MONTH) + 1;//获得月
            String mon = String.valueOf(month);//月转字符串
            int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);//获得当月最大日期
            int minDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);//获得当月最小日期
            for (int i = minDay; i <= maxDay; i++) {//循环当月所有天数
                list1.add(0);//当月无签到数据 每天都存0
                String md = mon + "-" + i;//拼接字符串 xx-x||xx-xx
                list2.add(md);//循环装入list2（日期集合）
            }
            map.put("worklist", list1);//map装入list1
            map.put("daylist", list2);//map装入list2
            return map;
        }
    }
}
